//
// Created by Felix on 24-4-24.
//

#ifndef GAZE_DATABASECOMPONENT_HPP
#define GAZE_DATABASECOMPONENT_HPP

#include "oatpp/core/macro/component.hpp"

#include "db/UserDb.hpp"
#include "db/LoginLogDb.hpp"
#include "db/CameraDb.hpp"

class DatabaseComponent {
public:
    OATPP_CREATE_COMPONENT(
            std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider
    )([] {
        auto connectionProvider = std::make_shared<oatpp::sqlite::ConnectionProvider>(DATABASE_FILE);
        return oatpp::sqlite::ConnectionPool::createShared(
                connectionProvider, 10 /* max-connections */, std::chrono::seconds(5) /* connection TTL */
        );
    }());
    OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
        OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
        auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
        return std::make_shared<UserDb>(executor);
    }());
    OATPP_CREATE_COMPONENT(std::shared_ptr<LoginLogDb>, loginLogDb)([] {
        OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
        auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
        return std::make_shared<LoginLogDb>(executor);
    }());
    OATPP_CREATE_COMPONENT(std::shared_ptr<CameraDb>, cameraDb)([] {
        OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
        auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
        return std::make_shared<CameraDb>(executor);
    }());
};

#endif //GAZE_DATABASECOMPONENT_HPP
